package com.letv.android.client.view;

import android.content.Context;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.letv.android.young.client.R;
//import com.letv.android.client.playerlibs.LetvSdkPlayerLibs;
//import com.letv.android.client.playerlibs.bean.MessageBeanPlayerLibs;
//import com.letv.android.client.playerlibs.utils.LetvToolsPlayerLibs;
//import com.letv.android.client.playerlibs.utils.LogInfoPlayerLibs;

//import com.letv.android.client.playerlibs.utils.NetWorkTypeUtilsPlayerLibs;

public class PublicLoadLayoutPlayerLibs extends FrameLayout {

	private RelativeLayout frameContent;
	private LinearLayout content;
	private View loading;
	private TextView loadingText;
	private View error;
	private static Context context;
	private TextView errorTxt1;
	private TextView errorTxt2;
	private ImageView errorImage;
	private RelativeLayout publicLoadLayout;
	private RefreshData mRefreshData;
	// 内容根布局是否是frameLayout，默认是线性布局
	private boolean contentIsFramelayout = false;

	private String mDefaultTextTabNoNetTextUrl;
	private String mDefaultTextTabNoDataTextUrl;
	private String mDefaultTextTabLoadingTextUrl;

	private Handler mHandler;

	public RefreshData getmRefreshData() {
		return mRefreshData;
	}

	public void setmRefreshData(RefreshData refreshData) {
		this.mRefreshData = refreshData;
	}

	public PublicLoadLayoutPlayerLibs(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public PublicLoadLayoutPlayerLibs(Context context) {
		super(context);
		init(context);
	}

	//从LetvToolsPlayerLibs.中提取，用在init中
	public static String getTextFromServer(String constantId, String defaultText) {
        String result = "";
      
        //MessageBeanPlayerLibs dialogMsgByMsg = LetvSdkPlayerLibs..getInstance().getDialogMsgByMsgId(constantId);
      /*  if (dialogMsgByMsg == null) {*/
            result = defaultText;
            //LogInfoPlayerLibs.log("carey", "constantId  " + constantId);
      /*  } else {
            result = dialogMsgByMsg.message;
           // LogInfoPlayerLibs.log("carey", "constantId  " + constantId + "  message " + result);
        }*/
     
        return result;
    }
	private void init(Context context) {
		PublicLoadLayoutPlayerLibs.context = context;
		inflate(context, R.layout.letv_public_loading_layout_playerlibs, this);
		mDefaultTextTabNoNetTextUrl = getTextFromServer(
				"500011",
				context.getString(R.string.load_data_no_net_playerlibs));
		mDefaultTextTabNoDataTextUrl = 
				getTextFromServer(
						"500012",
						context.getString(R.string.public_loading_net_errtxt2_datanull));
		mDefaultTextTabLoadingTextUrl = getTextFromServer(
				"500008", context.getString(R.string.public_loading_text));
		mHandler = new Handler();
		findView();
		setBackgroundColor(getResources().getColor(R.color.letv_color_fff6f6f6));
	}

	/**
	 * 添加内容到指定的LinearLayout中
	 * 
	 * @param viewId
	 */
	public void addContent(int viewId) {
		addContent(viewId, false);
	}

	/**
	 * 添加内容到布局中
	 * 
	 * @param viewId
	 * @param toFrameLayout
	 *            true:内容添加到FrameLayout中,false:添加到LinearLayout中
	 */
	public void addContent(int viewId, boolean toFrameLayout) {
		contentIsFramelayout = toFrameLayout;
		if (toFrameLayout) {
			inflate(getContext(), viewId, frameContent);
			frameContent.setVisibility(VISIBLE);
			content.setVisibility(View.GONE);
		} else {
			inflate(getContext(), viewId, content);
			frameContent.setVisibility(View.GONE);
			content.setVisibility(View.VISIBLE);
		}
	}

	public View getContentView() {
		return contentIsFramelayout ? frameContent : content;
	}

	private void findView() {
		publicLoadLayout = (RelativeLayout) findViewById(R.id.public_load_layout);
		errorImage = (ImageView) findViewById(R.id.net_error_flag);
		content = (LinearLayout) findViewById(R.id.content);
		loading = findViewById(R.id.loading_layout);
		loadingText = (TextView) findViewById(R.id.loadingText);
		error = findViewById(R.id.error);
		errorTxt1 = (TextView) findViewById(R.id.errorTxt1);
		errorTxt2 = (TextView) findViewById(R.id.errorTxt2);
		frameContent = (RelativeLayout) findViewById(R.id.frame_content);
		/*
		 * Add all area to be clickable
		 */
		error.setOnClickListener(mOnClickListener);
		// publicLoadLayout.setOnClickListener(mOnClickListener);
		// publicLoadLayout.setOnClickListener(mOnClickListener);
		// publicLoadLayout.setOnClickListener(mOnClickListener);
	}

	final Runnable mRunnable = new Runnable() {
		@Override
		public void run() {
			loading.setVisibility(View.GONE);
			error.setVisibility(View.VISIBLE);
			mHandler.removeCallbacks(mRunnable);
		}
	};

	OnClickListener mOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			//LogInfoPlayerLibs.log("hong",
				//	"PublicLoadLayoutPlayerLibs mOnClickListener");
			if (isNetAvailable()) {
				loading.setVisibility(View.VISIBLE);
				error.setVisibility(View.GONE);
				mHandler.postDelayed(mRunnable, 1000);
				return;
			} else {
				if (mRefreshData != null
						&& error.getVisibility() == View.VISIBLE) {
					mRefreshData.refreshData();
				}
			}
		}
	};

	public void loading(boolean isShowContent) {
		if (!TextUtils.isEmpty(mDefaultTextTabLoadingTextUrl)) {
			loadingText.setText(mDefaultTextTabLoadingTextUrl);
		} else {
			loadingText.setText(R.string.public_loading_text);
		}
		loading.setVisibility(VISIBLE);
		error.setVisibility(GONE);
		getContentView().setVisibility(isShowContent ? VISIBLE : GONE);
	}

	public void loadingforHot(boolean isShowContent) {
		if (!TextUtils.isEmpty(mDefaultTextTabLoadingTextUrl)) {
			loadingText.setText(mDefaultTextTabLoadingTextUrl);
		} else {
			loadingText.setText(R.string.public_loading_text);
		}
		error.setVisibility(GONE);
		loading.setVisibility(isShowContent ? GONE : VISIBLE);
		getContentView().setVisibility(isShowContent ? VISIBLE : GONE);
	}

	public void loading(boolean isShowContent, boolean shouLoading) {
		if (!TextUtils.isEmpty(mDefaultTextTabLoadingTextUrl)) {
			loadingText.setText(mDefaultTextTabLoadingTextUrl);
		} else {
			loadingText.setText(R.string.public_loading_text);
		}
		loading.setVisibility(GONE);
		error.setVisibility(GONE);
		getContentView().setVisibility(isShowContent ? VISIBLE : GONE);
	}

	public void finish() {
		loading.setVisibility(GONE);
		error.setVisibility(GONE);
		getContentView().setVisibility(VISIBLE);
	}

	public void finishError() {
		loading.setVisibility(GONE);
		error.setVisibility(GONE);
	}

	public void finishLayout() {
		loading.setVisibility(GONE);
		error.setVisibility(GONE);
		this.setVisibility(View.GONE);
	}

	public void finishLoad() {
		loading.setVisibility(GONE);
	}

	public void netError(boolean isShowContent) {
		loading.setVisibility(GONE);
		error.setVisibility(VISIBLE);
		error.setBackgroundColor(isShowContent ? Color.TRANSPARENT
				: getResources().getColor(R.color.letv_base_bg));
		getContentView().setVisibility(isShowContent ? VISIBLE : GONE);
		if (!TextUtils.isEmpty(mDefaultTextTabNoNetTextUrl)) {
			if (mDefaultTextTabNoNetTextUrl.contains("#")) {
				// eg:网络未连接#请检查网络后点击页面刷新
				String[] datas = mDefaultTextTabNoNetTextUrl.split("#");
				if (datas.length == 2) {
					errorTxt1.setText(datas[0]);
					errorTxt2.setText(datas[1]);
					return;
				}
			}
			errorTxt1.setText(mDefaultTextTabNoNetTextUrl);
		} else {
			errorTxt1.setText(mDefaultTextTabNoNetTextUrl);
		}
	}

	public void dataError(boolean isShowContent) {
		loading.setVisibility(GONE);
		error.setVisibility(VISIBLE);
		errorImage.setImageResource(R.drawable.letv_data_null);
		error.setBackgroundColor(isShowContent ? Color.TRANSPARENT
				: getResources().getColor(R.color.letv_base_bg));
		getContentView().setVisibility(isShowContent ? VISIBLE : GONE);

		if (!TextUtils.isEmpty(mDefaultTextTabNoDataTextUrl)) {
			if (mDefaultTextTabNoDataTextUrl.contains("#")) {
				String[] datas = mDefaultTextTabNoDataTextUrl.split("#");
				if (datas.length == 2) {
					errorTxt1.setText(datas[0]);
					errorTxt2.setText(datas[1]);
					return;
				}
			}
			errorTxt1.setText(mDefaultTextTabNoDataTextUrl);
		} else {
			errorTxt1.setText(R.string.public_loading_net_errtxt2_datanull);
		}
		/*
		 * Modified by chenhong, requested by gaipeng 2015-04-13 18:52 Added by
		 * pm gaipeng,数据加载失败时，页面提示不应该出现“无网络”的icon。
		 */
		// errorImage.setVisibility(View.GONE);
		// errorTxt1.setText(R.string.public_loading_net_errtext2_datanull_subtitle);
	}

	public void error(String errMsg) {
		error.setBackgroundColor(getResources().getColor(
				R.color.letv_color_fff6f6f6));
		loading.setVisibility(GONE);
		errorImage.setVisibility(GONE);
		error.setVisibility(VISIBLE);
		errorTxt1.setText(errMsg);
		getContentView().setVisibility(GONE);
	}

	public void showErrorMessage(String errMsg) {
		loading.setVisibility(GONE);
		errorImage.setVisibility(GONE);
		error.setVisibility(VISIBLE);
		error.setBackgroundColor(Color.TRANSPARENT);
		errorTxt1.setText(errMsg);
		getContentView().setVisibility(VISIBLE);
	}

	public void error(int errmsg) {
		loading.setVisibility(GONE);
		errorImage.setVisibility(GONE);
		error.setVisibility(VISIBLE);
		errorTxt1.setText(errmsg);
		errorTxt2.setVisibility(GONE);
		getContentView().setVisibility(GONE);
	}

	public void _error(int errmsg) {
		loading.setVisibility(GONE);
		errorImage.setVisibility(VISIBLE);
		error.setVisibility(VISIBLE);
		errorTxt1.setText(errmsg);
	}

	// 聊天室
	public void chatError(int errmsg) {
		loading.setVisibility(GONE);
		error.setVisibility(VISIBLE);
		errorImage.setImageResource(R.drawable.letv_chat_error_normal);
		errorTxt1.setText(errmsg);
		errorTxt2.setVisibility(GONE);
		error.setBackgroundColor(getResources().getColor(R.color.letv_base_bg));
		getContentView().setVisibility(VISIBLE);
	}

	// 暂无相关视频
	public void dataNull(int errmsg, int errIcon) {
		loading.setVisibility(GONE);
		error.setVisibility(VISIBLE);
		errorImage.setImageResource(errIcon);
		errorTxt1.setText(errmsg);
		errorTxt2.setVisibility(GONE);
		error.setBackgroundColor(getResources().getColor(R.color.letv_base_bg));
		getContentView().setVisibility(VISIBLE);
	}

	public void setErrorBackgroundColor(int color) {
		error.setBackgroundColor(color);
	}

	public TextView getLoadingText() {
		return loadingText;
	}

	public void finishHalfPlay() {
		loading.setVisibility(GONE);
		getContentView().setVisibility(VISIBLE);
	}

	public interface RefreshData {

		void refreshData();
	}
//===========================================================
	//从PublicLoadLayoutPlayerLibs提取，为了isNetAvailable
	public static NetworkInfo getAvailableNetWorkInfo() {
		NetworkInfo activeNetInfo = null;
		try {
			ConnectivityManager connectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			activeNetInfo = connectivityManager.getActiveNetworkInfo();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		if (activeNetInfo != null && activeNetInfo.isAvailable()) {
			return activeNetInfo;
		} else {
			return null;
		}
	}
	//从PublicLoadLayoutPlayerLibs提取，为了OnClickListener
	public static boolean isNetAvailable() {
		return getAvailableNetWorkInfo() == null;
	}
}
